package com.ygqh.baby.interceptor;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.utils.DateConvertUtils;

public class QueryInfoArgumentResolver implements HandlerMethodArgumentResolver {

	@Override
	public boolean supportsParameter(MethodParameter parameter) {
		Class<?> type = parameter.getParameterType();
		if (type.equals(Date.class) ) {
			return true;
		} else if (type.equals(QueryInfo.class)) {
			return true;
		}
		return false;
	}

	@Override
	public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
			WebDataBinderFactory binderFactory) throws Exception {
		Class<?> type = methodParameter.getParameterType();
		if (type.equals(Date.class)) {
			String paramName = methodParameter.getParameterName();
			String[] values = webRequest.getParameterValues(paramName);
			List<Object> finalParamValuesList = new ArrayList<Object>();
			if (values != null) {
				for (String val : values) {
					Date date = this.dateConverter(val);
					finalParamValuesList.add(date);
				}
			}
			return finalParamValuesList.get(0);
		} else if (type.equals(QueryInfo.class)) {
			return extractQueryInfo(methodParameter, webRequest);
		}
		return null;
	}

	private Date dateConverter(String value) {

		try {
			return DateUtils.parseDate(value, DateConvertUtils.DATE_TIME_FORMAT, DateConvertUtils.DATE_FORMAT);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;

	}

	private QueryInfo extractQueryInfo(MethodParameter methodParameter, NativeWebRequest webRequest) {
		HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
		String start = request.getParameter("start");
		String limit = request.getParameter("limit");
		String page = request.getParameter("page");
		String modifyTime = request.getParameter("modifyTime");
		String iDisplayStart = request.getParameter("iDisplayStart");
		String iDisplayLength = request.getParameter("iDisplayLength");
		String startId = request.getParameter("startId");
		String endId = request.getParameter("endId");

		Integer i_start = null;
		Integer i_limit = null;
		Integer i_page = null;
		if (StringUtils.isNotEmpty(start)) {
			i_start = Integer.parseInt(start);
		}
		if (StringUtils.isNotEmpty(limit)) {
			i_limit = Integer.parseInt(limit);
		}
		if (StringUtils.isNotEmpty(page)) {
			i_page = Integer.parseInt(page);
		}
		if (StringUtils.isNotEmpty(iDisplayStart)) {
			i_start = Integer.parseInt(iDisplayStart);
		}
		if (StringUtils.isNotEmpty(iDisplayLength)) {
			i_limit = Integer.parseInt(iDisplayLength);
		}
		request.setAttribute("start", start);
		request.setAttribute("limit", limit);
		request.setAttribute("page", page);
		request.setAttribute("startId", startId);
		request.setAttribute("endId", endId);
		request.setAttribute("modifyTime", modifyTime);

		int defaultLimit = 20;

		if (i_start == null && i_limit == null && i_page == null)
			return new QueryInfo(0, defaultLimit);

		QueryInfo queryInfo = null;

		if (i_page == null) {
			if (i_limit == null) {
				queryInfo = new QueryInfo(0, defaultLimit);
			}
			if (i_start == null) {
				i_start = 0;
			}
		}
		if (queryInfo == null)
			queryInfo = new QueryInfo(i_start, i_limit, i_page, modifyTime);

		queryInfo.setLimit(i_limit);
		queryInfo.setStartId(startId);
		queryInfo.setEndId(endId);

		// 配置排序信息
		String sortKey = request.getParameter("sortKey");
		queryInfo.setSortKey(sortKey);

		return queryInfo;
	}

}
